package demo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhouWeiBo
 * @create 2022/6/8
 */
public class Mediation implements IMediator{
    private List<Person> people = new ArrayList<>();

    public Mediation() {}

    private Mediation(List<Person> people) {
        this.people = people;
    }

    @Override
    public void register(Person person) {
        this.people.add(person);
    }

    @Override
    public void pair(Person person) {
        Person pairPerson = null;
        System.out.println("正在按照您的要求，查找符合的另一半!");
        for (Person p : people){
            if (match(p,person)) {
                pairPerson = p;
                break;
            }
        }
        if (pairPerson != null){
            System.out.println("配对成功，祝福" + pairPerson.getName() + "和"+person.getName() + "相爱终生！");
            people.remove(pairPerson);
        }else{
            System.out.println("抱歉啊，没找到跟你适合的！");
        }
    }

    /**
     * 判断是否般配
     * @param p1
     * @param p2
     * @return
     */
    private boolean match(Person p1,Person p2){

        return p1.getAge().equals(p2.getRequireAge()) &&

               p2.getAge().equals(p1.getRequireAge()) &&

               p1.getSex() != p2.getSex();
    }
}
